package com.ivsign.lib_idcardidentification;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import com.clj.fastble.BleManager;
import com.clj.fastble.callback.BleNotifyCallback;
import com.clj.fastble.callback.BleWriteCallback;
import com.clj.fastble.data.BleDevice;
import com.clj.fastble.exception.BleException;

import java.io.IOException;

/**
 * @description :
 * @autHor :  Jason
 * @date : 2018/8/16 10:24
 */

public class IDCardController extends Handler {

    private static final String LOGTAG = "BLELOG";
    private BleDevice bleDevice;

    private DeviceUUID deviceUUID;

    private IReadListener iReadListener;

    private Context context;
    private final BleWriteCallback mWriteRsp = new BleWriteCallback() {
        @Override
        public void onWriteSuccess(int current, int total, byte[] justWrite) {
            //CommonUtils.toast(ReadIDByBleActivity.this, "success");
        }

        @Override
        public void onWriteFailure(BleException exception) {
            // CommonUtils.toast(ReadIDByBleActivity.this, "failed");
        }


    };
    private final CoreProcesser coreProcesser;
    private final SocketCommunication socketCommunication;

    public IDCardController(Looper looper, BleDevice bleDevice, DeviceUUID deviceUUID, IReadListener iReadListener, Context context) {
        super(looper);
        this.bleDevice = bleDevice;
        this.deviceUUID = deviceUUID;
        this.iReadListener = iReadListener;
        this.context = context;

        IDCardCommunication idCardCommunication = new IDCardCommunication(bleDevice, deviceUUID, new BleWriteCallback() {

            @Override
            public void onWriteSuccess(int current, int total, byte[] justWrite) {

                Log.e("tag", "onWriteSuccess: " + HexUtil.encodeHexStr(justWrite));
            }

            @Override
            public void onWriteFailure(BleException exception) {

                Log.e("tag", "onWriteFailure: " + exception.getDescription());
            }
        },context);
        MainHandler mainHandler = new MainHandler(iReadListener);
        socketCommunication = new SocketCommunication(idCardCommunication, mainHandler);
        coreProcesser = new CoreProcesser(socketCommunication);

    }

    public void readID() {
        openNotify();
    }

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        if (msg.what == 1) {
            byte[] value = (byte[]) (msg.obj);
            coreProcesser.handByte(value);
        }
    }


    private void openNotify() {
        BleManager.getInstance().notify(bleDevice, deviceUUID.getService(), deviceUUID.getNotifyChracter(), new BleNotifyCallback() {

            @Override
            public void onNotifySuccess() {

            }

            @Override
            public void onNotifyFailure(BleException exception) {

            }

            @Override
            public void onCharacteristicChanged(byte[] data) {
                Message message = new Message();
                message.what = 1;
                message.obj = data;
                IDCardController.this.sendMessage(message);
            }
        });

    }


    public void reset() {
        coreProcesser.reset();
    }

    public void close() {
        try {
            if (socketCommunication != null) {

                socketCommunication.CloseTcpsocket();
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
        if (coreProcesser!=null){
            coreProcesser.close();
        }
    }
}



